package com.leji.song.mine;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;

import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import com.dgl.sdk.util.DBStore;
import com.leji.song.R;
import com.leji.song.extra.BaseActivity;
import com.leji.song.mine.model.DS_Address;
import com.leji.song.util.BaiduMapUtil;
import com.leji.song.util.ConstAns;

import java.util.List;

public class AdressManagerActivity extends BaseActivity{
    private Button addAdress;
    private ListView addressListView;
    private TextView myAdress;
    private BroadcastReceiver broadcastReceiver;
    private View myAdressView;

    @Override
	protected void onCreate(Bundle savedInstanceState) {
		setAnimType(RIGHT_ENTER_LEFT_EXT);
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.mine_adress_manager);
        initView();

        getAddress();
        getLocationAddress();
	}

    private void getAddress() {
        List<DS_Address>  addressList = DBStore.readAll(DS_Address.class, ConstAns.DB_TABLE_MY_ADDRESS);
        AdressAdapter adressAdapter = new AdressAdapter(this, addressList);
        addressListView.setAdapter(adressAdapter);
    }

    private void initView() {

        setActivityTitle("管理送餐地址");
        setReturnBtn();


        addAdress = (Button) findViewById(R.id.addAdress);
        addressListView = (ListView) findViewById(R.id.addressListView);
        myAdress = (TextView) findViewById(R.id.myAdress);
        myAdressView = (View) findViewById(R.id.myAdressView);

        if(!BaiduMapUtil.myAdress.equals("")){
            myAdress.setText(BaiduMapUtil.myAdress);
        }

        addAdress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(AdressManagerActivity.this, AddAddressActivity.class);
                startActivityForResult(intent, 0);
            }
        });

        myAdressView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(AdressManagerActivity.this, AddAddressActivity.class);
                if(!BaiduMapUtil.myAdress.equals("")){
                    intent.putExtra("address", BaiduMapUtil.myAdress);
                }
                startActivityForResult(intent, 0);
            }
        });


    }


    private void  getLocationAddress() {

        broadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {

                String locationString = intent.getStringExtra("location");
                if((locationString!= null) && (!locationString.equals(""))){
                    myAdress.setText(locationString);
                }
            }
        };

        registerReceiver(broadcastReceiver, new IntentFilter(ConstAns.MSG_LOCATION_CHANGE));
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        getAddress();
    }

    @Override
    public void finish() {
        unregisterReceiver(broadcastReceiver);
        super.finish();
    }
}
